/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.reader.service;

import com.thinkgem.jeesite.common.persistence.DataEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.reader.dao.ReaderRolerDao;
import com.thinkgem.jeesite.modules.reader.dao.ReadersDao;
import com.thinkgem.jeesite.modules.reader.entity.Reader;
import com.thinkgem.jeesite.modules.reader.entity.ReaderRoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 读者管理
 *
 * @author zhangc
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class ReadersService extends CrudService<ReadersDao, Reader>{

	@Autowired
	private ReaderRolerDao readerRolerDao;

	/**
	 * 获取用户
	 * @param id
	 * @return
	 */
	public Reader getUser(String id) {
		return dao.get(id);
	}


	/**
	 * 根据读者的图书借阅证号查询读者信息
	 * @param readerCardNo
	 * @return
	 */
	public Reader getReaderInfoBySn(String readerCardNo) {
		return dao.getReaderInfoBySn(readerCardNo);
	}
	/**
	 * 更新读者状态（挂失或停用）
	 * @param
	 * @return
	 */
	@Transactional(readOnly = false)
	public int changeStatus(Reader reader) {
		return  dao.changeStatus(reader);
	}

	public Page<Reader> findUser(Page<Reader> page, Reader reader) {
		// 设置分页参数
		reader.setPage(page);
		// 执行分页查询
		page.setList(dao.findList(reader));
		return page;
	}

	@Transactional()
	public void saveReader(Reader reader){
		if(StringUtils.isBlank(reader.getId())){
			reader.preInsert();
			reader.setRdId(reader.getId());
			dao.insert(reader);
		}
		else{
			reader.preUpdate();
			dao.update(reader);
		}
	}

	@Transactional()
	public void deleteReader(Reader reader) {
		reader.preUpdate();
		reader.setDelFlag(DataEntity.DEL_FLAG_DELETE);
		dao.delete(reader);
	}

	public ReaderRoler findReaderRoleByRdid(Reader reader){
		return readerRolerDao.findReaderRoleByRdid(reader);
	}

    public long findAllCount(){
        return dao.findAllCount(null);
    }

    public Reader getReaderInfoByCardNo(String cardNo){
    	return dao.getReaderInfoByCardNo(cardNo);
    }

	public Reader getReaderInfoByMobile(String mobile){
		return dao.getReaderInfoByMobile(mobile);
	}

	@Transactional()
	public Result importReader(Reader reader){
		String rdSex = reader.getRdSex();
		switch(rdSex){
			case "男":
				rdSex = "1";
				break;
			case "女":
				rdSex = "2";
				break;
		}
		reader.setRdSex(rdSex);

		String rdState = reader.getRdState();
		switch(rdState){
			case "正常":
				rdState = "0";
				break;
			case "停用":
				rdState = "1";
				break;
			case "挂失":
				rdState = "2";
				break;
		}
		reader.setRdState(rdState);

		String rdRoleId = reader.getRdRoleId();
		ReaderRoler role = readerRolerDao.getByRoleName(rdRoleId);
		if(null == role){
			return Result.error(",不存在的角色:" + rdRoleId);
		}
		else{
			reader.setRdRoleId(role.getRdRoleId());

			this.saveReader(reader);
			return Result.success(Result.SUCCESS_MSG);
		}
	}
}
